package com.tvsinfo.jdk.demo.case6_higher_order_function;

import java.lang.annotation.*;
import java.util.*;
import java.util.function.BiPredicate;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class HigerOrder2 {


    public void queryOrder(OrderInfoQueryRequest orderInfoQueryRequest) {

        List<OrderInfoDetailsResponse> orderInfoDetailsResponses = new ArrayList<>();
        Stream<OrderInfoDetailsResponse> stream = orderInfoDetailsResponses.stream();

        Function<OrderInfoDetailsResponse, OrderBuyerSnapshot> function = OrderInfoDetailsResponse::getBuyerSnapshot;
        Function<OrderBuyerSnapshot, Long> function2=OrderBuyerSnapshot::getWid;

        //wid
        filter(stream,
                OrderInfoDetailsResponse::getBuyerSnapshot,
                OrderBuyerSnapshot::getWid,
                orderInfoQueryRequest::getWid,
                Long::equals);

        //buyerPhone
        filter(stream, OrderInfoDetailsResponse::getBuyerSnapshot, OrderBuyerSnapshot::getName, orderInfoQueryRequest::getBuyerPhone, String::equals);

        //receiverPhone
        filter(stream, OrderInfoDetailsResponse::getDeliverAddressSnapshot, OrderDeliverAddressSnapshot::getReceiverName, orderInfoQueryRequest::getReceiverPhone, String::equals);

        //goodsName 多条件查询
        filter(stream, OrderInfoDetailsResponse::getOrderDetailList, List::stream, orderInfoQueryRequest::getGoodsName,
                (orderDetailStream, s) -> {
                    Optional optional = filter(orderDetailStream, OrderDetail::getGoodsName, Function.identity(), s::toString, (r1, u) -> r1.equals(u)).findAny();
                    return optional.isPresent();
                }
        );

        //结果处理
        List<OrderInfoDetailsResponse> filterResult = stream.collect(Collectors.toList());
    }


    public class FilterCompareExtract<T, R, R1, U> {
        private Function<T, R> sourceExtract;
        private Function<R, R1> compareSource;
        private Supplier<U> queryResult;

        private FilterCompareExtract filterCompareExtract;

//        <T, R, R1, U> FilterCompareExtract getInstance(Function<T, R> sourceExtract, Function<R, R1> compareSource, Supplier<U> queryResult) {
//
//            filterCompareExtract.getInstance()
//        }

        public Function<T, R> getSourceExtract() {
            return sourceExtract;
        }

        public void setSourceExtract(Function<T, R> sourceExtract) {
            this.sourceExtract = sourceExtract;
        }

        public Function<R, R1> getCompareSource() {
            return compareSource;
        }

        public void setCompareSource(Function<R, R1> compareSource) {
            this.compareSource = compareSource;
        }

        public Supplier<U> getQueryResult() {
            return queryResult;
        }

        public void setQueryResult(Supplier<U> queryResult) {
            this.queryResult = queryResult;
        }
    }


    /**
     * 过滤
     */
    private <T, R, R1, U> Stream<T> filter(Stream<T> stream,
                                           Function<T, R> sourceExtract,
                                           Function<R, R1> compareSource,
                                           Supplier<U> queryResult,
                                           BiPredicate<R1, U> comparePredicate) {
        U u = queryResult.get();
        if (null == u) {
            return stream;
        }
        return stream.filter(t -> test(t, sourceExtract, compareSource, queryResult, comparePredicate));
    }

    /**
     * 确认值
     */
    private <T, R, R1, U> boolean test(T t,
                                       Function<T, R> sourceExtract,
                                       Function<R, R1> compareSource,
                                       Supplier<U> queryResult,
                                       BiPredicate<R1, U> comparePredicate) {
        R r = sourceExtract.apply(t);
        if (null == r) {
            return false;
        }
        R1 r1 = compareSource.apply(r);
        if (null == r1) {
            return false;
        }
        return comparePredicate.test(r1, queryResult.get());
    }


    /**
     * 查询对象
     */
    private class OrderInfoQueryRequest {

        private Long wid;
        private String buyerPhone;
        private String receiverPhone;
        private String goodsName;
        private String orderSourceDesc;
        private Collection<Integer> inAppChannelTypes;

        public Long getWid() {
            return wid;
        }

        public void setWid(Long wid) {
            this.wid = wid;
        }

        public String getBuyerPhone() {
            return buyerPhone;
        }

        public void setBuyerPhone(String buyerPhone) {
            this.buyerPhone = buyerPhone;
        }

        public String getReceiverPhone() {
            return receiverPhone;
        }

        public void setReceiverPhone(String receiverPhone) {
            this.receiverPhone = receiverPhone;
        }

        public String getGoodsName() {
            return goodsName;
        }

        public void setGoodsName(String goodsName) {
            this.goodsName = goodsName;
        }

        public String getOrderSourceDesc() {
            return orderSourceDesc;
        }

        public void setOrderSourceDesc(String orderSourceDesc) {
            this.orderSourceDesc = orderSourceDesc;
        }

        public Collection<Integer> getInAppChannelTypes() {
            return inAppChannelTypes;
        }

        public void setInAppChannelTypes(Collection<Integer> inAppChannelTypes) {
            this.inAppChannelTypes = inAppChannelTypes;
        }
    }

    /**
     * 返回对象
     */
    private class OrderInfoDetailsResponse {
        /**
         * 订单明细
         **/
        private List<OrderDetail> orderDetailList;

        /**
         * 【买家】买家快照
         */
        private OrderBuyerSnapshot buyerSnapshot;

        /**
         * 【买家】配送地址快照
         */
        private OrderDeliverAddressSnapshot deliverAddressSnapshot;

        /**
         * 订单备注
         */
        private List<OrderRemarkRespDto> remarks;

        /**
         * 订单来源
         */
        private OrderSourceSnapshot orderSourceSnapshot;


        public OrderInfoDetailsResponse() {
        }

        public List<OrderDetail> getOrderDetailList() {
            return orderDetailList;
        }

        public void setOrderDetailList(List<OrderDetail> orderDetailList) {
            this.orderDetailList = orderDetailList;
        }

        public OrderBuyerSnapshot getBuyerSnapshot() {
            return buyerSnapshot;
        }

        public void setBuyerSnapshot(OrderBuyerSnapshot buyerSnapshot) {
            this.buyerSnapshot = buyerSnapshot;
        }

        public OrderDeliverAddressSnapshot getDeliverAddressSnapshot() {
            return deliverAddressSnapshot;
        }

        public void setDeliverAddressSnapshot(OrderDeliverAddressSnapshot deliverAddressSnapshot) {
            this.deliverAddressSnapshot = deliverAddressSnapshot;
        }

        public List<OrderRemarkRespDto> getRemarks() {
            return remarks;
        }

        public void setRemarks(List<OrderRemarkRespDto> remarks) {
            this.remarks = remarks;
        }

        public OrderSourceSnapshot getOrderSourceSnapshot() {
            return orderSourceSnapshot;
        }

        public void setOrderSourceSnapshot(OrderSourceSnapshot orderSourceSnapshot) {
            this.orderSourceSnapshot = orderSourceSnapshot;
        }
    }


    /**
     * 订单详情对象
     */
    private class OrderDetail {
        /**
         * 商品名称
         **/
        private String goodsName;

        public String getGoodsName() {
            return goodsName;
        }

        public void setGoodsName(String goodsName) {
            this.goodsName = goodsName;
        }
    }

    /**
     * 买家快照
     */
    private class OrderBuyerSnapshot {
        /**
         * 【买家】买家微盟ID
         */
        private Long wid;
        /**
         * 【买家】买家姓名
         */
        private String name;

        public Long getWid() {
            return wid;
        }

        public void setWid(Long wid) {
            this.wid = wid;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }

    /**
     * 配送地址快照
     */
    private class OrderDeliverAddressSnapshot {
        /**
         * 【收货】收货人
         */
        private String receiverName;

        /**
         * 【收货】收货手机
         */
        private String receiverPhone;

        public String getReceiverName() {
            return receiverName;
        }

        public void setReceiverName(String receiverName) {
            this.receiverName = receiverName;
        }

        public String getReceiverPhone() {
            return receiverPhone;
        }

        public void setReceiverPhone(String receiverPhone) {
            this.receiverPhone = receiverPhone;
        }
    }

    /**
     * 订单备注
     */
    private class OrderRemarkRespDto {

    }

    /**
     * 订单来源
     */
    private class OrderSourceSnapshot {

    }

//    @Target(ElementType.FIELD)
//    @Retention(RetentionPolicy.RUNTIME)
//    @Documented
//    public @interface DBEquel {
//
//
//        trFunction();
//
//        rr1Function();
//
//        predicate();
//    }

}
